package com.hd.service.activiti.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.activiti.ActStartRight;
import com.hd.service.activiti.ActStartRightService;
import com.hd.service.sys.OrgService;
import com.hd.service.sys.RoleService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;

/**
 * 流程启动权限
 */
@Service("actStartRightService")
public class ActStartRightServiceImpl implements ActStartRightService {
	@Autowired
	private BaseDao<ActStartRight, String> actStartRightDao;
	@Autowired
	private RoleService roleService;
	@Autowired
	private OrgService organizationService;

	@Override
	public void save(ActStartRight actStartRight) {
		actStartRight.setId(UUID.randomUUID().toString());
		actStartRightDao.save(actStartRight);
	}


	@Override
	public void delete(String id) {
		actStartRightDao.delete(ActStartRight.class, id);
	}

	@Override
	public void update(ActStartRight actStartRight) {
		actStartRightDao.update(actStartRight);
	}

	@Override
	public ActStartRight get(String id) {
		return actStartRightDao.get(ActStartRight.class, id);
	}

	@Override
	public void selectDataGrid(PageInfo pageInfo) {
		String hql = "from ActStartRight where 1=1 ";
		Map<String, Object> condition = pageInfo.getCondition();
		List<Object> params = new ArrayList<Object>();
		if (!pageInfo.getSort().equals("")) {
			hql += " order by " + pageInfo.getSort() + " " + pageInfo.getOrder();
		}
		List<ActStartRight> actStartRights = actStartRightDao.findPage(hql, pageInfo.getNowpage(),
				pageInfo.getPagesize(), params);
		pageInfo.setRows(actStartRights);
		String countHql="select count(*)  "+hql;
		int total=Integer.parseInt(actStartRightDao.getCountByList(countHql,params).toString());
		pageInfo.setTotal(total);
	}

	@Override
	public ActStartRight getByFK(String procdefid) {
		String hql = "from ActStartRight where 1=1 and procdefid=?";
		List<ActStartRight> startRights = actStartRightDao.find(hql, procdefid);
		if (startRights.size() > 0) {
			return startRights.get(0);
		}
		return null;
	}

	@Override
	public Boolean hasAuthenticated(String procdefid, String userid) {
		Boolean b = true;
		ActStartRight actStartRight = getByFK(procdefid);
		if (actStartRight != null) {
			if (StringUtils.isNotNull(actStartRight.getUserids())) {
				if (!actStartRight.getUserids().contains(userid)) {
					b = false;
				}
			}
			if (StringUtils.isNotNull(actStartRight.getRoleids())) {
				String userids = roleService.getUserids(actStartRight.getRoleids());
				if (!userids.contains(userid)) {
					b = false;
				}
			}
			if (StringUtils.isNotNull(actStartRight.getDeptids())) {
				String userids = organizationService.getUserids(actStartRight.getDeptids());
				if (!userids.contains(userid)) {
					b = false;
				}
			}
		}
		return b;
	}

}
